package com.jprocms.module.cms.convert.content;

import com.jpro.framework.common.pojo.PageResult;
import com.jprocms.module.cms.controller.admin.content.vo.ContentAttrCreateReqVO;
import com.jprocms.module.cms.controller.admin.content.vo.ContentAttrRespVO;
import com.jprocms.module.cms.controller.admin.content.vo.ContentAttrUpdateReqVO;
import com.jprocms.module.cms.dal.dataobject.content.ContentAttrDO;
import com.jprocms.module.cms.dal.dataobject.model.ModelDO;
import com.jprocms.module.cms.util.ModelWrapper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 内容自定义数据 Convert
 *
 * @author jprocms
 */
@Mapper
public interface ContentAttrConvert {

    ContentAttrConvert INSTANCE = Mappers.getMapper(ContentAttrConvert.class);

    ContentAttrDO convert(ContentAttrCreateReqVO bean);

    ContentAttrDO convert(ContentAttrUpdateReqVO bean);

    ContentAttrRespVO convert(ContentAttrDO bean);

    List<ContentAttrRespVO> convertList(List<ContentAttrDO> list);

    PageResult<ContentAttrRespVO> convertPage(PageResult<ContentAttrDO> page);

    default ContentAttrDO convert(ContentAttrCreateReqVO bean, Long channelId) {
        ContentAttrDO attrDO = convert(bean);
        attrDO.setContentId(channelId);
        return attrDO;
    }

    ;

    default ContentAttrDO convert(ContentAttrUpdateReqVO bean, Long channelId) {
        ContentAttrDO attrDO = convert(bean);
        attrDO.setContentId(channelId);
        return attrDO;
    }

    ;

    default List<ContentAttrDO> disassembleCustoms(ModelDO model, Long id, Map<String, Object> customs) {
        List<ContentAttrDO> list = new ArrayList<>();
        ModelWrapper modelWrapper = new ModelWrapper(model);
        modelWrapper.disassembleCustoms(customs, (name, type, value) -> list.add(new ContentAttrDO(id, name, type, value)));
        return list;
    }


}
